#include "customwidgets.h"

KeyPushButton::KeyPushButton(QWidget* parent=0) : parent(parent) {}

void KeyPushButton::setKeys(const QStringList keys) {
  this->keys = keys;
  
  switch (keys.count()) {
    case 1:
      setText(keys[0]);
      break;
    case 2:
      setText(keys[1] + "+" + keys[0]);
      break;
    case 3:
      setText(keys[1] + "+" + keys[2] + "+" + keys[0]);
      break;
  }
}

QStringList KeyPushButton::getKeys() {
  return keys;
}

KeyCaptureLine::KeyCaptureLine(QWidget* parent) : ignoreMods(false), ignoreRegs(false) {}

void KeyCaptureLine::ignoreModifiers(bool enable=false) { ignoreMods = enable; }
void KeyCaptureLine::ignoreRegulars(bool enable=false) { ignoreRegs = enable; }

bool KeyCaptureLine::x11Event(XEvent* event) {
  if (event->type == KeyPress) {
    KeySym ks = XLookupKeysym(&event->xkey, 0);
    if (ks == NoSymbol)
      return true;
    else if (ignoreMods && IsModifierKey(ks))
      return true;
    else if (ignoreRegs && !IsModifierKey(ks))
      return true;
    
    char* key = XKeysymToString(ks);
    setText(QString(key));
    return true;
  }
  
  return false;
}
